#!/usr/bin/env python
#

import webapp2
import random
from xml.dom.minidom import Document
class MainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/xml'
        doc = Document()
        wml = doc.createElement("sensor_readings")
        doc.appendChild(wml)
        #temp
        temp = doc.createElement("sensor")
        temp.setAttribute("type", "temperature")
        param = doc.createElement("param")
        param.setAttribute("value",str(random.gauss(23,2)))
        temp.appendChild(param)
        wml.appendChild(temp)
        #velocity
        velocity = doc.createElement("sensor")
    	velocity.setAttribute("type","velocity")
    	param = doc.createElement("param")
        param.setAttribute("value",str(random.gauss(70,20)))
        velocity.appendChild(param)
    	wml.appendChild(velocity)
    	#humidity
    	humidity = doc.createElement("sensor")
    	humidity.setAttribute("type","humidity")
    	param = doc.createElement("param")
        param.setAttribute("value",str(random.gauss(70,10)))
        humidity.appendChild(param)
    	wml.appendChild(humidity)
    	#location
    	location = doc.createElement("sensor")
    	location.setAttribute("type","location")
    	param = doc.createElement("param")
        param.setAttribute("value",str(random.gauss(48,1)))
        param.setAttribute("name","longitude")
        location.appendChild(param)
        param = doc.createElement("param")
        param.setAttribute("value",str(random.gauss(14,1)))
        param.setAttribute("name","latitude")
        location.appendChild(param)
    	wml.appendChild(location)
    	#acceleration
    	acceleration = doc.createElement("sensor")
    	acceleration.setAttribute("type","acceleration")
    	param = doc.createElement("param")
        param.setAttribute("value",str(random.random()))
        param.setAttribute("name","x")
        acceleration.appendChild(param)
        param = doc.createElement("param")
        param.setAttribute("value",str(random.random()))
        param.setAttribute("name","y")
        acceleration.appendChild(param)
        param = doc.createElement("param")
        param.setAttribute("value",str(random.random()))
        param.setAttribute("name","z")
        acceleration.appendChild(param)
    	wml.appendChild(acceleration)
        self.response.write(doc.toprettyxml(indent="  "))
class ViewHandler(webapp2.RequestHandler):
    def get(self):
        template="""<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
<p id="result"></p>
 

<script>


(function worker() {

 $.ajax({
        type: "GET",
    url: "/",
    dataType: "xml",
    success: function(xml) {
        $('#result').empty();
        var html="";
        $(xml).find('sensor').each(function(){
            var id = $(this).attr('type');

            //$('#result').append('<h3>');
            //$('#result').append(id);
            //$('#result').append("</h3></br>");
            html=html.concat('<h3>',id,'</h3></br>');
            $(this).find('param').each(function(){
                var name=$(this).attr('name');
                var value=$(this).attr('value');
                if(name){
                    html=html.concat(name,"=");
                    //$('#result').append(name);
                    //$('#result').append("=");
                    }
                html=html.concat(value,"</br>");
                //$('#result').append(value);
                //$('#result').append("</br>");
                });

      
                    
        });
    $('#result').append(html);
    setTimeout(worker, 2000);
    }
});
})();

 
</script>
</body>
</html>
"""
        self.response.write(template)        

app = webapp2.WSGIApplication([
    ('/', MainHandler),('/view',ViewHandler)
], debug=True)
